package drongo.desktop.selector
{
	import flash.events.EventDispatcher;
	
	[Event(name="SELECTLIST_CHANGE", type="drongo.desktop.selector.SelectorEvent")]
	[Event(name="CLIPBOARD_CHANGE", type="drongo.desktop.selector.SelectorEvent")]
	public class Selector extends EventDispatcher
	{
		private var _selectedList:Array;
		private var _clipboard:Array;
		public function Selector()
		{
			super();
			_selectedList=new Array();
			_clipboard=new Array();
		}
		/**
		 * 将选择列表中的内容复制到剪切板 
		 */		
		public function copy(dispatch:Boolean=true):void{
			_clipboard.length=0;
			for each (var i:Object in _selectedList) 
			{
				_clipboard.push(i);
			}
			if(dispatch){
				dispatchClipboardEvent();
			}
		}
		/**
		 * 添加内容到选择列表(内部会去重)
		 * @param value
		 */		
		public function push(value:Object,dispatch:Boolean=true):void{
			if(value is Array||value is Vector){
				for each (var i:Object in value) 
				{
					push(i,false);
				}
			}else{
				var index:int=_selectedList.indexOf(value);
				if(index>=0){
					return;
				}
				_selectedList.push(value);
			}
			if(dispatch){
				dispatchSelectListEvent();
			}
		}
		public function indexOf(value:Object):int{
			return _selectedList.indexOf(value);
		}
		/**
		 * 删除某个内容 
		 * @param index
		 * @param ispatch
		 */		
		public function remvoeAt(index:int,dispatch:Boolean=true):void{
			_selectedList.splice(index,1);
			if(dispatch){
				dispatchSelectListEvent();
			}
		}
		public function get length():uint{
			return _selectedList.length;
		}
		/**
		 * 清空选择列表 
		 */		
		public function resetSelectList(dispatch:Boolean=true):void{
			if(_selectedList.length==0){
				return;
			}
			_selectedList.length=0;
			if(dispatch){
				dispatchSelectListEvent();
			}
		}
		/**
		 * 清空剪切板 
		 */		
		public function resetClipboard(dispatch:Boolean=true):void{
			if(_clipboard.length==0){
				return;
			}
			_clipboard.length=0;
			if(dispatch){
				dispatchClipboardEvent();
			}
		}
		/**
		 * 选择列表 
		 * @return 
		 */		
		public function get  selectedList():Array{
			return _selectedList;	
		}
		/**
		 * 快速获取选择列表中的第一个内容 
		 * @return 
		 */		
		public function get fristItem():Object{
			if(_selectedList.length==0){
				return null;
			}
			return _selectedList[0];
		}
		/**
		 * 快速获取选择列表中的最后一个内容 
		 * @return 
		 */		
		public function get endItem():Object{
			if(_selectedList.length==0){
				return null;
			}
			return _selectedList[_selectedList.length - 1];
		}
		/**
		 * 剪切板 
		 * @return 
		 */		
		public function get clipboard():Array{
			return _clipboard;
		}
		private function dispatchSelectListEvent():void{
			this.dispatchEvent(new SelectorEvent(SelectorEvent.SELECTLIST_CHANGE));
		}
		private function dispatchClipboardEvent():void{
			this.dispatchEvent(new SelectorEvent(SelectorEvent.CLIPBOARD_CHANGE));
		}
		//=====================================属性记录=======================================//
		public function getPropertyList(propertys:Array):Array{
			var list:Array=[];
			var data:Object;
			for each (var selectData:Object in selectedList) 
			{
				data={target:selectData};
				for each (var propertyKey:String in propertys) 
				{
					data[propertyKey]=selectData[propertyKey];
				}
				list.push(data);
			}
			return list;
		}
	}
}